<template>
  <div id="page-wrapper" class="gray-bg">
    <header-link/>
    <div class="row wrapper border-bottom white-bg page-heading">
      <div class="col-sm-4">
        <h2>修改密码</h2>
        <ol class="breadcrumb">
          <li>
            <router-link :to="{ name: 'home' }">
              <a>主页</a>
            </router-link>
          </li>
          <li class="active">
            <strong>修改密码</strong>
          </li>
        </ol>
      </div>
    </div>

        <div class="wrapper container wrapper-content animated fadeInUp">
          <div class="ibox">
            <div class="ibox-title">
              <h5></h5>
              <div class="ibox-tools">
                <router-link :to="{ name: 'userDetail'}" class="btn btn-primary btn-xs">
                  <small><i class="fa fa-undo"></i></small> 返回
                </router-link>
              </div>
            </div>
              <div class="ibox-content">
                <form method="get" class="form-horizontal">
                  <!-- <div class="form-group has-error"> -->
                  <div class="form-group">
                    <label class="col-sm-3 control-label">用户名</label>
                    <div class="col-sm-3">
                      <p class="form-control-static">{{ userName }}</p>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-3 control-label">密码</label>
                    <div class="col-sm-3">
                      <input type="password" class="form-control" v-model="passwordOld">
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-3 control-label">新密码</label>
                    <div class="col-sm-3">
                      <input type="password" class="form-control" v-model="passwordNew">
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="col-sm-3 control-label">重复新密码</label>
                    <div class="col-sm-3">
                      <input type="password" class="form-control" v-model="passwordNew1">
                    </div>
                  </div>

                  <div class="hr-line-dashed"></div>
                  <div class="form-group">
                    <div class="col-sm-4 col-sm-offset-3">
                      <a class="btn btn-primary" v-on:click="changePassword" >修改</a>
                    </div>
                  </div>
                </form>
              </div>
          </div>
        </div>
  </div>

</template>

<script>
import header from '@/components/Header.vue'
import cookie from '@/server/cookie.js'
import server from '@/server/server.js'
export default {
  name: 'changePassword',
  data () {
    return {
      id: '',
      userName: '',
      passwordOld: '',
      passwordNew: '',
      passwordNew1: ''
    }
  },
  components: {
    'header-link': header
  },
  created () {
    const vue = this
    vue.userName = cookie.getCookie("name")
    vue.id = cookie.getCookie("id")
  },
  mounted () {
  },
  methods: {
    changePassword: function() {
      const vue = this
      if (vue.passwordOld == '' || vue.passwordNew == '' || vue.passwordNew1 == '') {
        vue.$toastr('warning', '参数异常', 'warning')
        return false
      } else if (vue.passwordNew != vue.passwordNew1) {
        vue.$toastr('warning', '两次密码不一致', 'warning')
        return false
      }

      const params = {
        id: vue.id,
        passwordOld: vue.passwordOld,
        passwordNew: vue.passwordNew
      }

      server.post(server.postChangePasswordUrl, null, params, true, false, this, function(data) {
        vue.$toastr('success', '请重新登陆', '密码修改成功')
        // 跳转登录页面
        vue.$router.push({ name: 'login'})
      })

    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#page-wrapper {
  margin: 0;
}
</style>
